﻿
//Author: 张亚涛
//createtime:2016-01-04 22:19:16
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "WashPirce" 表的方法;
    /// </summary>
    public class WashPirceBase
    {
        const string SQL_Select = "select [ID],[Project],[Pirce],[Times] from [WashPirce] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回WashPirce表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<WashPirce> GetAllWashPirce()
        {
            List<WashPirce> washPirceList = new List<WashPirce>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                WashPirce washPirce = new WashPirce(reader);
                washPirceList.Add(washPirce);
            }
            reader.Close();
            return washPirceList;
        }
        /// <summary>
        /// 根据WashPirce表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static WashPirce GetWashPirceById(int id)
        {
            WashPirce washPirce = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                washPirce = new WashPirce(reader);
            }
            reader.Close();
            return washPirce;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据WashPirce表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeleteWashPirceById(int id)
        {
            string sql = "delete from [WashPirce] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(WashPirce washPirce)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",washPirce.Id),
            new SqlParameter("@Project",washPirce.Project),
            new SqlParameter("@Pirce",washPirce.Pirce),
            new SqlParameter("@Times",washPirce.Times)    
            };
            return paras;
        }
        /// <summary>
        /// 根据WashPirce表的ID字段更新数据 
        /// </summary> 
        /// <param name="WashPirce">washPirce</param>
        /// <returns></returns>       
        public static int UpdateWashPirceById(WashPirce washPirce)
        {
            string sql = "update [WashPirce] set [Project] = @Project,[Pirce] = @Pirce,[Times] = @Times where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(washPirce);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向WashPirce表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="washPirce">WashPirce</param>       
        /// <returns></returns>        
        public static int AddWashPirce(WashPirce washPirce)
        {
            string sql = "insert into [WashPirce]([Project],[Pirce],[Times])values(@Project,@Pirce,@Times)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(washPirce);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回WashPirce表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllWashPirce_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}